package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.dao.mapper.BillExceptionMapper;
import cn.tedu.charging.order.dao.mapper.BillFailMapper;
import cn.tedu.charging.order.dao.mapper.BillSuccessMapper;
import cn.tedu.charging.order.dao.repository.BillRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillExceptionPO;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BillRepositoryImpl implements BillRepository {
    @Autowired
    private BillFailMapper billFailMapper;
    @Autowired
    private BillSuccessMapper billSuccessMapper;
    @Autowired
    private BillExceptionMapper billExceptionMapper;

    @Override
    public void saveFailBill(ChargingBillFailPO fail) {
        billFailMapper.insert(fail);
    }

    @Override
    public void saveSuccessBill(ChargingBillSuccessPO success) {
        billSuccessMapper.insert(success);
    }

    @Override
    public Boolean existsBill(String orderNo) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bill_id", orderNo);
        //successCount=select count() from success where bill_id=?
        long sucCount=billSuccessMapper.selectCount(queryWrapper);
        //failCount=select count() from fail where bill_id=?
        long failCount=billFailMapper.selectCount(queryWrapper);
        //successCount>0||failCount>0
        return sucCount>0||failCount>0;
    }

    @Override
    public ChargingBillSuccessPO getSuccessByBillId(String billId) {
        //select * from success where bill_id=#{}
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bill_id", billId);
        return billSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveExceptionBill(ChargingBillExceptionPO exceptionPO) {
        billExceptionMapper.insert(exceptionPO);
    }

    @Override
    public void updateSuccessStatus(String billId, int i) {
        //update success set status=3 where bill_id=#{}
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("bill_id", billId);
        updateWrapper.set("bill_status", i);
        billSuccessMapper.update(updateWrapper);
    }
}
